package com.jomoo.account.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.smartcardio.CardException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jomoo.account.dao.AccountdDao;
import com.jomoo.account.service.AccountService;
import com.jomoo.common.exception.AccountException;
import com.jomoo.common.pojo.JomooAccount;
import com.jomoo.common.tools.DateTools;
import com.jomoo.common.utils.Constants;

/**
 * 下拉菜单逻辑接口实现
 * @author haosc
 */
@Service("accountServiceImpl")
@Transactional(rollbackFor = AccountException.class)
public class AccountServiceImpl implements AccountService
{

	@Resource
	public AccountdDao accountDao;
	
	/**
	 * 账目录入
	 * 
	 * @param iType
	 * @param fNum
	 * @param strAccountTime
	 * @param strAccountRemark
	 * @return
	 * @throws RemarkException
	 */
	@Override
	public Integer addAccount(Integer iType, Float fNum, String strAccountTime,
			String strAccountRemark) throws AccountException
	{
		// 时间格式化
		SimpleDateFormat simpl = new SimpleDateFormat(Constants.DATE_TIME_HHMM);

		JomooAccount account = new JomooAccount();

		Integer iAcId = null;

		try
		{
			account.setAccountType(iType);
			account.setAccountTime(simpl.parse(strAccountTime));

			if (1 == iType)
			{
				account.setAccountSale(fNum);
			}
			else
			{
				account.setAccountSale(-fNum);
			}

			account.setActive(1);
			account.setIsBalance(1);
			account.setRemark(strAccountRemark);

			iAcId = accountDao.saveAllObj(account);

		}
		catch (Exception e)
		{
			new AccountException("保存账目信息异常：" + e.getMessage());
		}
		return iAcId;
	}

	/**
	 * 查询账目信息
	 * 
	 * @param iType
	 * @param iState
	 * @param fstartNum
	 * @param fendNum
	 * @param strBeginTime
	 * @param strEndTime
	 * @return
	 * @throws CardException
	 */
	@Override
	public List<JomooAccount> getServiceCards(Integer iType, Integer iState, Float fstartNum,
			Float fendNum, String strBeginTime, String strEndTime, String strFlag)
			throws AccountException
	{
		List<JomooAccount> accountList = null;

		SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_TIME_PATTERN);
		
		try
		{
			Date newDate = DateTools.getAfterDay(strEndTime);
			
			Date dStartDate = format.parse(strBeginTime);
			
			if(StringUtils.isEmpty(strFlag))
			{
				accountList = accountDao.getAccountExcel(iType, iState, fstartNum, fendNum, dStartDate, newDate);
			}
			else 
			{
				// 获取账单信息
				accountList = accountDao.getAccounts(iType, iState, fstartNum, fendNum, dStartDate, newDate);
			}
			
		}
		catch (Exception e)
		{
			new AccountException("查询账目异常：" + e.getMessage());
		}
		return accountList;
	}

	/**
	 * 作废账单信息
	 * @param iId
	 * @return
	 * @throws RemarkException
	 */
	@Override
	public Integer deleteAccount(Integer iId) throws AccountException
	{
		Integer iCount = null;

		try
		{
			// 作废账单
			iCount = accountDao.deleteAccount(iId);

		}
		catch (Exception e)
		{
			new AccountException("作废账目信息异常：" + e.getMessage());
		}
		return iCount;
	}
	
	/**
	 * 获取当月产品各自安装数量用于图表显示
	 * @return
	 * @throws RemarkException
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List getMonthAccountView() throws AccountException
	{
		List viewList = null;

		try
		{
			Date starDate = DateTools.getStartTime();
			
			Date endDate = DateTools.getEndTime();
			
			// 查询本月图表数据
			viewList = accountDao.getMonthAccountView(starDate, endDate);

		}
		catch (Exception e)
		{
			new AccountException("作废账目信息异常：" + e.getMessage());
		}
		return viewList;
	}
	
	/**
	 * 按账单选择类型按需生成图表
	 * @param iType
	 * @param iCustomer
	 * @param iSetupType
	 * @param iProduct
	 * @param iSpec
	 * @param iSetupState
	 * @param strBeginTime
	 * @param strEndTime
	 * @return
	 * @throws AccountException
	 */
	@SuppressWarnings("rawtypes")
	@Override
	public List getAccountViewCreate(Integer iType,Integer iCustomer, 
										Integer iSetupType, Integer iProduct, 
												Integer iSpec, Integer iSetupState, 
													String strBeginTime, String strEndTime) throws AccountException
	{
		List viewList = null;

		try
		{
			Date endDate = DateTools.getAfterDay(strEndTime);
			
			
			// 按需查询账单信息并生成图表
			viewList = accountDao.getAccountsView(iType, iCustomer, iSetupType, iProduct, 
																	iSpec, iSetupState, strBeginTime, endDate);
		}
		catch (Exception e)
		{
			new AccountException("账单图表信息查询异常：" + e.getMessage());
		}
		return viewList;
	}
	
	
}
